﻿using System.Text.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Xml;
using WWLauncher.Model;
using WWLauncher.ViewModel;
using System.Text.Json;
using System.Collections.ObjectModel;
using System.Collections;
using System.Dynamic;

namespace WWLauncher.Util
{
    class SvgToGeometry
    {

        public static List<IconfontModel> GetIconfonts()
        {
            string svgPath = "/Resource/Iconfont/iconfont.js";
            string jsonPath = "/Resource/Iconfont/iconfont.json";

            Stream svgStream = Application.GetResourceStream(new Uri(svgPath, UriKind.Relative)).Stream;
            Stream jsonStream = Application.GetResourceStream(new Uri(jsonPath, UriKind.Relative)).Stream;

            StreamReader streamReader = new StreamReader(svgStream);
            string svgJsStr = streamReader.ReadToEnd();


            StreamReader streamReader2 = new StreamReader(jsonStream);
            string jsonStr = streamReader2.ReadToEnd();

            return GetIconfonts(svgJsStr, jsonStr);
        }


        public static List<IconfontModel> GetIconfonts(string svgJsStr, string jsonStr)
        {

            svgJsStr = svgJsStr.Substring(svgJsStr.IndexOf("<svg>"),
                svgJsStr.Length - (svgJsStr.Length - (svgJsStr.IndexOf("</svg>") + "</svg>".Length)) - svgJsStr.IndexOf("<svg>"));

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(svgJsStr);
            XmlNodeList nodeList = xmlDoc.SelectNodes("/svg/symbol");


            var obj = JsonSerializer.Deserialize<Root>(jsonStr);
            var ja = obj.glyphs;
            List<IconfontModel> listInfo = new List<IconfontModel>();
            for (int i = 0; i < nodeList.Count; i++)
            {
                XmlNodeList pathNodes = nodeList[i].SelectNodes("path");
                string text = "";
                foreach (XmlNode pathNode in pathNodes)
                {
                    text += pathNode.Attributes["d"].Value;
                }
                string name = ja[i].name;
                listInfo.Add(new IconfontModel
                {
                    Text = text,
                    Name = name
                });
            }

            return listInfo;
        }

        public class GlyphsItem
        {
            public string name { get; set; }
         
        }

        public class Root
        {
            public List<GlyphsItem> glyphs { get; set; }
        }
    }
}